#!/bin/bash

# 获取完整的 CentOS 版本号，例如 7.6.1810
version=$(cat /etc/redhat-release | grep -oP '\d+\.\d+\.\d+')

# 输出版本号
echo "您当前的系统版本为 CentOS $version"

# 使用 Tsinghua 的镜像源
baseurl="https://mirrors.tuna.tsinghua.edu.cn/centos-vault/$version/os/x86_64/"

# 备份原有的 repo 文件
echo "正在备份原有的 repo 文件..."
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

# 创建新的 repo 文件
echo "正在创建新的 repo 文件..."
cat > /etc/yum.repos.d/CentOS-Base.repo <<EOF
[base]
name=CentOS-$version - Base
baseurl=$baseurl
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$version - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/$version/updates/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$version - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/$version/extras/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF

# 清理缓存并更新
echo "正在清理缓存并更新..."
yum clean all
yum makecache

echo "换源完成。"
